package com.hfzy.ihk.web.aliyuncall.support.cloudcall.base;

import com.hfzy.ihk.facade.aliyuncall.CallRecordBaseEntity;
import com.hfzy.ihk.web.aliyuncall.support.disruptor.base.DisruptorEvent;
import com.hfzy.ihk.web.aliyuncall.support.disruptor.base.DisruptorHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by YANFA on 2018/7/31.
 */
public abstract class AbstractRecordFileHandler <T extends CallRecordBaseEntity,E extends DisruptorEvent<T>> implements DisruptorHandlerAdapter<E> {
    Logger logger = LoggerFactory.getLogger(AbstractRecordFileHandler.class);
    @Override
    public void onEvent(E event, long sequence, boolean endOfBatch) throws Exception {
        try {
            Long old = System.currentTimeMillis();
          //  procces(event.getData());
            old = System.currentTimeMillis()-old;
            logger.info("{}  eventHandler handle use time:{}",old);
        } catch (Exception e) {
            logger.error("AbstractRecordFileHandler exception",e);
        }

    }

    @Override
    public void onEvent(E event) throws Exception {

        try {
            Long old = System.currentTimeMillis();

            procces(event.getData());

            old = System.currentTimeMillis()-old;
            logger.info("{} workHandler handle use time:{}",old);
        } catch (Exception e) {
            logger.error("AbstactTaoBaoCallStateHandler exception",e);
        }
    }

    protected void procces(T state){

        String recordFileUrl = null;

        //将录音URL解码
        recordFileUrl = getRecordFileUrl(state);
        //处理数据

        if(recordFileUrl==null){
            return ;
        }
        //处理推送过来的录音url
        dealData(state,recordFileUrl);



    }

    /**
     * 获取url
     * @param data
     */
    protected  abstract String getRecordFileUrl(T data);



    /**
     * 更新reids 数据或者推送到ES
     */
    protected abstract void dealData(T data,String recordFileUrl);
}


